//var Lib = {
//phoneRex: /^[1][3-9][0-9]{9}$/,
//smsCodeRex: /^[0-9]{6}$/,
//passWordRex: /^[0-9a-zA-Z]{6,20}$/,
//url: "http://vip.CBEC.ink/index.php/qweras/Api/entry"
//}

var sms = null
var smsCode = Vue.component('sms-code',{
  template:'<div class="sms-code"><mu-text-field :errorText="errorText" v-model="currentValue" fullWidth placeholder="请输入短信验证码" /><mu-button class="commonbtns" @click="getSMSCode" :disabled="canSend" flat>{{codeMsg}}</mu-button></div>',
  mounted: function () {
    if (localStorage.SMSCodeStratTime) {
      this.sended = true;
      this.startTime = localStorage.SMSCodeStratTime;
      sms.sending();
    }
  },
  created: function () {
    sms = this;
  },
  methods: {
    getSMSCode: function () {
      sms.sended = true;
      sms.$emit('getcode')
      
     
    },
    sending: function () {
      let now = new Date().getTime();
      let time = Math.round((now - sms.startTime) / 1000);
      sms.codeMsg = `稍等${sms.time - time}秒`
      if (time >= sms.time) {
        sms.sended = false;
        sms.startTime = null;
        sms.timer = null;
        sms.codeMsg = `获取验证码`
        localStorage.removeItem('SMSCodeStratTime')
      } else {
        sms.timer = setTimeout(function () {

          sms.sending();

        }, 1000)
      }

    }
  },
  data: function () {
    return {
      codeMsg: '获取验证码',
      sended: false,
      timer: null,
      startTime: null,
    }
  },
  props: {
    phone: {
      type: String,
      require: true,
    },
    opens: {
      type: Boolean,
      require: true,
    },
    time: {
      type: Number,
      require: true,
      default: 60
    },
    value: {
      default: ''
    },
    errorText: {
      default: ''
    }
  },
  computed: {
  	phonePass: function () {
      return /^[1][3-9][0-9]{9}$/.test(sms.phone);
    },
    canSend: function () {
      return !(sms.phonePass && !sms.sended);
    },
    currentValue: {
      get: function () {
        return this.value
      },
      set: function (val) {
        return this.$emit('input', val)
      }
    }
  },
  watch:{
  	opens(){
  		//console.log(sms.opens)
  		 if(sms.opens){
      	sms.startTime = new Date().getTime();
	      localStorage.setItem('SMSCodeStratTime', sms.startTime);
	      sms.sending()
     } 
  	}
  }
})
